//
//  PickerViewTest4ViewController.swift
//  iOS-RxSwift-Tutorials_Example
//
//  Created by mengru.tian on 2020/9/22.
//  Copyright © 2020 CocoaPods. All rights reserved.
//

import UIKit
import RxSwift
import RxDataSources

class PickerViewTest4ViewController: UIViewController {

    var pickerView:UIPickerView!
    
    //设置自定义视图的pickerView适配器
    private let viewPickerAdapter = RxPickerViewViewAdapter<[UIColor]>(
        components: [],
        numberOfComponents: { _,_,_  in 1 },
        numberOfRowsInComponent: { (_, _, items, _) -> Int in
            return items.count}
        ){ (_, _, items, row, _, view) -> UIView in
            let componentView = view ?? UIView()
            componentView.backgroundColor = items[row]
            return componentView
        }
    
    let disposeBag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        title = "38.4.使用自定义视图"
        view.backgroundColor = .white
        
        test()
    }
    
    func test() {
        //创建pickerView
        pickerView = UIPickerView()
        self.view.addSubview(pickerView)
         
        //绑定pickerView数据
        Observable.just([UIColor.red, UIColor.orange, UIColor.yellow])
            .bind(to: pickerView.rx.items(adapter: viewPickerAdapter))
            .disposed(by: disposeBag)
    }

}
